package com.ht.utils;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.payload.APNPayload.DictionaryAlertMsg;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.ht.api.ISysBaseAPI;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * APP推送消息
 *
 * @author gaozhihua
 */
@Component
public class AppPushUtil {

    private static ISysBaseAPI sysBaseAPI;

    public static ISysBaseAPI getSysBaseAPI() {
        return sysBaseAPI;
    }


    public void setSysBaseAPI(ISysBaseAPI sysBaseAPI) {
        AppPushUtil.sysBaseAPI = sysBaseAPI;
    }

    public static String pushMessageToSingle(String title, String content, String cid) {
        Map<String, Object> uniqueConfig = sysBaseAPI.getUniqueConfig();
        String appId = (String) uniqueConfig.get("getui_app_id");
        String appKey = (String) uniqueConfig.get("getui_app_key");
        String masterSecret = (String) uniqueConfig.get("getui_master_secret");
        String url = (String) uniqueConfig.get("getui_url");
        if (Strings.isBlank(appId) || Strings.isBlank(appKey) || Strings.isBlank(masterSecret) || Strings.isBlank(url)) {
            return null;
        }
        // STEP1：创建push对象
        IGtPush push = new IGtPush(url, appKey, masterSecret);
        Style0 style = new Style0();
        // STEP2：设置推送标题、推送内容
        style.setTitle(title);
        style.setText(content);
        // style.setBigStyle2(content); // Style6可选参数
        style.setLogo("push.png"); // 设置推送图标
        // STEP3：设置响铃、震动等推送效果
        style.setRing(true); // 设置响铃
        style.setVibrate(true); // 设置震动
        // STEP4：选择通知模板
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setStyle(style);
        template.setTransmissionType(1);
        template.setAPNInfo(getAPNPayload(title, content));
        // STEP5 设置message类型
        SingleMessage singleMessage = new SingleMessage();
        singleMessage.setData(template);
        singleMessage.setOffline(true);
        singleMessage.setOfflineExpireTime(1800 * 1000);
        // STEP6：执行推送
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        IPushResult ret = push.pushMessageToSingle(singleMessage, target);
        return ret.getResponse().toString();
    }

    public static String pushMessageToList(String title, String content, List<String> cidList) {
        Map<String, Object> uniqueConfig = sysBaseAPI.getUniqueConfig();
        String appId = (String) uniqueConfig.get("getui_app_id");
        String appKey = (String) uniqueConfig.get("getui_app_key");
        String masterSecret = (String) uniqueConfig.get("getui_master_secret");
        String url = (String) uniqueConfig.get("getui_url");
        if (Strings.isBlank(appId) || Strings.isBlank(appKey) || Strings.isBlank(masterSecret) || Strings.isBlank(url)) {
            return null;
        }
        // STEP1：创建push对象
        IGtPush push = new IGtPush(url, appKey, masterSecret);
        Style0 style = new Style0();
        // STEP2：设置推送标题、推送内容
        style.setTitle(title);
        style.setText(content);
        // style.setBigStyle2(content); // Style6可选参数
        style.setLogo("push.png"); // 设置推送图标
        // STEP3：设置响铃、震动等推送效果
        style.setRing(true); // 设置响铃
        style.setVibrate(true); // 设置震动
        // STEP4：选择通知模板
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setStyle(style);
        template.setTransmissionType(1);
        template.setAPNInfo(getAPNPayload(title, content));
        List<Target> targets = new ArrayList<>();
        // STEP5 设置message类型
        ListMessage message = new ListMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        message.setPushNetWorkType(0);

        // STEP6：执行推送
        if (cidList == null || cidList.size() == 0) {
            return "系统异常,请填写cidList";
        }
        for (String cid : cidList) {
            Target target = new Target();
            target.setAppId(appId);
            target.setClientId(cid);
            targets.add(target);
        }
        String taskId = push.getContentId(message, "任务别名");
        IPushResult ret = push.pushMessageToList(taskId, targets);
        return ret.getResponse().toString();
    }

    public static String pushMessageToAll(String title, String content) {
        Map<String, Object> uniqueConfig = sysBaseAPI.getUniqueConfig();
        String appId = (String) uniqueConfig.get("getui_app_id");
        String appKey = (String) uniqueConfig.get("getui_app_key");
        String masterSecret = (String) uniqueConfig.get("getui_master_secret");
        String url = (String) uniqueConfig.get("getui_url");
        if (Strings.isBlank(appId) || Strings.isBlank(appKey) || Strings.isBlank(masterSecret) || Strings.isBlank(url)) {
            return null;
        }
        // STEP1：创建push对象
        IGtPush push = new IGtPush(url, appKey, masterSecret);
        Style0 style = new Style0();
        // STEP2：设置推送标题、推送内容
        style.setTitle(title);
        style.setText(content);
        style.setLogo("push.png"); // 设置推送图标
        // STEP3：设置响铃、震动等推送效果
        style.setRing(true); // 设置响铃
        style.setVibrate(true); // 设置震动
        // STEP4：选择通知模板
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setStyle(style);
        template.setTransmissionType(1);
        template.setAPNInfo(getAPNPayload(title, content));
        // STEP5 设置message类型
        List<String> appIds = new ArrayList<>();
        appIds.add(appId);
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600); // 时间单位为毫秒
        // STEP6：执行推送
        IPushResult ret = push.pushMessageToApp(message);
        return ret.getResponse().toString();
    }

    private static APNPayload getAPNPayload(String title, String content) {
        APNPayload payload = new APNPayload();
        // 在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
//        payload.setAutoBadge("+1");
        payload.setContentAvailable(0);
        // ios 12.0 以上可以使用 Dictionary 类型的 sound
        payload.setSound("default");
//        payload.setCategory("$由客户端定义");
//        payload.addCustomMsg("由客户自定义消息key", "由客户自定义消息value");
        DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
        alertMsg.setBody(content);
        alertMsg.setTitle(title);   //通知标题
        payload.setAlertMsg(alertMsg);
        // 设置语音播报类型，int类型，0.不可用 1.播放body 2.播放自定义文本
        payload.setVoicePlayType(0);
        return payload;
    }

}
